package com.laolang.shop.modules.product.web.admin;

import com.laolang.shop.common.core.domain.Page;
import com.laolang.shop.common.core.domain.SimpleAjax;
import com.laolang.shop.modules.product.entity.ProductCategory;
import com.laolang.shop.modules.product.logic.admin.ProductCategoryAdminLogic;
import com.laolang.shop.modules.product.pojo.category.admin.dto.ProductCategoryAdminEditDto;
import com.laolang.shop.modules.product.pojo.category.admin.vo.ProductCategoryAdminTreeVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author khlbat
 * @version 1.0
 * @date 2020/11/1 20:46
 */
@AllArgsConstructor
@RequestMapping("admin/product/category")
@RestController
public class ProductCategoryAdminController {

    private final ProductCategoryAdminLogic productCategoryAdminLogic;

    @GetMapping("info")
    public SimpleAjax<Boolean> info() {
        return SimpleAjax.ok(true);
    }

    @GetMapping("loadTreeLazy/{parentId}")
    public SimpleAjax<List<ProductCategoryAdminTreeVo>> loadTreeLazy(@PathVariable Long parentId) {
        return SimpleAjax.ok(productCategoryAdminLogic.loadTreeLazy(parentId));
    }

    @GetMapping("page")
    public SimpleAjax<Page<ProductCategory>> page(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                  @RequestParam(value = "parentId", defaultValue = "0") Long parentId) {
        return SimpleAjax.ok(productCategoryAdminLogic.page(page, size, parentId));
    }

    @PostMapping("add")
    public SimpleAjax<Boolean> add(@RequestBody ProductCategoryAdminEditDto dto) {
        return SimpleAjax.ok(productCategoryAdminLogic.add(dto));
    }

    @PostMapping("edit")
    public SimpleAjax<Boolean> edit(@RequestBody ProductCategoryAdminEditDto dto) {
        return SimpleAjax.ok(productCategoryAdminLogic.edit(dto));
    }

    @PostMapping("del/{id}")
    public SimpleAjax<Boolean> del(@PathVariable Long id) {
        return SimpleAjax.ok(productCategoryAdminLogic.del(id));
    }


}
